package com.ruoyi.checkingin.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.checkingin.controller.ERpRecordController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.e_checkingin.EActuallyWage;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.temporary.PayOffEntity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.enterprise.domain.EProfession;
import com.ruoyi.enterprise.service.IEProfessionService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.checkingin.mapper.EActuallyWageMapper;
import com.ruoyi.checkingin.service.IEActuallyWageService;

/**
 * 实发工资Service业务层处理
 *
 * @author JYW
 * @date 2024-03-04
 */
@Service
public class EActuallyWageServiceImpl implements IEActuallyWageService
{
    @Autowired
    private EActuallyWageMapper eActuallyWageMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ERpRecordController rpRecordController;

    @Autowired
    private IEProfessionService professionService;


    /**
     * 查询实发工资
     *
     * @param id 实发工资主键
     * @return 实发工资
     */
    @Override
    public EActuallyWage selectEActuallyWageById(Long id)
    {
        return eActuallyWageMapper.selectEActuallyWageById(id);
    }

    /**
     * 查询实发工资列表
     *
     * @param eActuallyWage 实发工资
     * @return 实发工资
     */
    @Override
    public List<EActuallyWage> selectEActuallyWageList(EActuallyWage eActuallyWage)
    {
        return eActuallyWageMapper.selectEActuallyWageList(eActuallyWage);
    }

    /**
     * 新增实发工资
     *
     * @return 结果
     */
    @Override
    public int insertEActuallyWage()
    {
            List<SysUser> sysUsers = userService.selectUserList(new SysUser());
            //1.遍历users,为每位员工添加实际工资记录
            String date = DateUtils.parseDateToStr("yyyy-MM", new Date());
            for (SysUser sysUser : sysUsers){
                Long userId = sysUser.getUserId();

                Map<String, Object> map = rpRecordController.forecastWage(userId, new Date());
                Map<String, BigDecimal> map1 = professionService.selectRespectWage((EProfession) map.get("wageDetial"));
                //2.填入工资详情计算数据
                EActuallyWage actuallyWage = new EActuallyWage();
                actuallyWage.setuId(userId);
                actuallyWage.setBasicWage(map1.get("salary"));
                actuallyWage.setRewardMoney((BigDecimal) map.get("rMoney"));
                actuallyWage.setPunishmentMoney((BigDecimal) map.get("pMoney"));
                actuallyWage.setAllowance(map1.get("addtion"));
                actuallyWage.setInsurance(map1.get("fund"));
                actuallyWage.setWageSum(map1.get("wageSum"));
                actuallyWage.setPublishTime(date);

                //判断新增还是修改
                List<EActuallyWage> actuallyWages = eActuallyWageMapper.selectEActuallyWageListByTime(userId, date);
                if (!actuallyWages.isEmpty()){
                    actuallyWage.setId(actuallyWages.get(0).getId());
                    eActuallyWageMapper.updateEActuallyWage(actuallyWage);
                }else {
                    eActuallyWageMapper.insertEActuallyWage(actuallyWage);
                }
            }

        return 1;
    }

    /**
     * 修改实发工资
     *
     * @param eActuallyWage 实发工资
     * @return 结果
     */
    @Override
    public int updateEActuallyWage(EActuallyWage eActuallyWage)
    {
        eActuallyWage.setUpdateTime(DateUtils.getNowDate());
        return eActuallyWageMapper.updateEActuallyWage(eActuallyWage);
    }

    /**
     * 批量删除实发工资
     *
     * @param ids 需要删除的实发工资主键
     * @return 结果
     */
    @Override
    public int deleteEActuallyWageByIds(Long[] ids)
    {
        return eActuallyWageMapper.deleteEActuallyWageByIds(ids);
    }

    /**
     * 删除实发工资信息
     *
     * @param id 实发工资主键
     * @return 结果
     */
    @Override
    public int deleteEActuallyWageById(Long id)
    {
        return eActuallyWageMapper.deleteEActuallyWageById(id);
    }

    @Override
    public Map<String,Object> allPayOff(String date) {
        List<PayOffEntity> payOffEntities = new ArrayList<>();
        //1.获取所有部门信息
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        //2.遍历部门id分别查询到部门下的员工list
        for (SysDept sysDept : sysDepts){
            PayOffEntity payOffEntity = new PayOffEntity();
            List<EActuallyWage> actuallyWages = eActuallyWageMapper.allPayOff(sysDept.getDeptId(), date);
            if (!actuallyWages.isEmpty()){
                payOffEntity.setActuallyWages(actuallyWages);
                //部门工资求和
                payOffEntity.setDeptSum(actuallyWages
                        .stream()
                        .map(EActuallyWage::getWageSum)
                        .reduce(BigDecimal.ZERO, BigDecimal::add));
            }else {
                payOffEntity.setActuallyWages(new ArrayList<>());
                payOffEntity.setDeptSum(BigDecimal.ZERO);
            }
            payOffEntity.setSysDept(sysDept);
            payOffEntities.add(payOffEntity);
        }
        //3.遍历员工list,分别获取对应的实际工资记录
        //4.计算各部门工资总和、以及全部总和
        Map<String,Object> map = new HashMap<>();
        map.put("deptInfo",payOffEntities);
        map.put("allSum",payOffEntities
                .stream()
                .map(PayOffEntity::getDeptSum)
                .reduce(BigDecimal.ZERO,BigDecimal::add));
        return map;
    }
}
